Completed
Push — master ( d362a9...56faa4 )
by Mark
14s queued 11s
created

BelongsTo.setName   A

Complexity

Conditions 5

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 5
dl 0
loc 5
rs 9.3333
c 0
b 0
f 0
1
import Relation from "../Relation.js";
2
import {ModelStaticInterface} from "../../../JeloquentInterfaces";
3
4
export default class BelongsTo extends Relation {
5
6
    constructor(model: ModelStaticInterface, foreignKey: string|null = null, name: string) {
7
        super(model, (foreignKey ?? `${model.snakeCaseClassName}_id`), name);
8
    }
9
10
    get originalValue() {
11
        return this.model.find(this.$parent[`original_${this.foreignKey}`]);
12
    }
13
14
    get value() {
15
        return this.model.find(this.$parent[this.foreignKey]);
16
    }
17
18
    // eslint-disable-next-line @typescript-eslint/no-empty-function
19
    set value(value) {
20
21
    }
22
23
    setName(): BelongsTo {
24
        const className = this.model.snakeCaseClassName;
25
        this.$name = this.$name ?? `${className}`;
26
        return this;
27
    }
28
29
    protected setParentProperties() {
30
        super.setParentProperties();
31
32
        let name = '';
33
        for (const namePart of this.$name.split('_')) {
34
            name += namePart[0].toUpperCase() + namePart.slice(1);
35
        }
36
37
        Object.defineProperty(this.$parent,
38
            `has${name}`, {
39
                get: () => {
40
                    return this.value !== null;
41
                },
42
            }
43
        )
44
45
        return this;
46
    }
47
}